
#region Using Statements
using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

using Elephant.Utility.Components;

using Elephant.Framework;
using Elephant.Framework.Services;

namespace SpinningPyramid
{
    [ComponentDependency(typeof(Mesh))]
    public class PyramidRenderer : DrawableContent
    {
        LookAtCamera camera;

        Mesh mesh;
        Transform transform;

        public override void Initialize(bool addDependencies)
        {
            base.Initialize(addDependencies);

            mesh = Owner.Find<Mesh>();
            transform = Owner.Find<Transform>();
        }
        
        public override void Start()
        {
            camera = Owner.Manager.FindWith<LookAtCamera>().Find<LookAtCamera>();
        }
        
        public override void LoadGraphicsContent(bool loadAllContent)
        {
            mesh.AssetName = "pyramid";
        }

        public override void Draw(GameTime gameTime)
        {
            // garbage in this loop
            foreach (ModelMesh mm in mesh.Model.Meshes) {
                foreach (BasicEffect effect in mm.Effects) {
                    effect.EnableDefaultLighting();

                    effect.View = camera.View;
                    effect.Projection = camera.Projection;

                    effect.World =
                        mesh.Transforms[mm.ParentBone.Index] *
                        transform.World;
                }

                mm.Draw();
            }
        }
    }
}
